  // Main application entry point
var PresidentsDataStore;         // this will be our datastore
var PresidentsColumnModel;       // this will be our columnmodel
var PresidentListingEditorGrid;
var PresidentListingWindow;

Ext.onReady(function(){
  Ext.QuickTips.init();
  PresidentsDataStore = new Ext.data.Store({
      id: 'PresidentsDataStore',
      proxy: new Ext.data.HttpProxy({
                url: 'database.php',      // File to connect to
                method: 'POST'
            }),
            baseParams:{task: "LISTING"}, // this parameter asks for listing
      reader: new Ext.data.JsonReader({   
                  // we tell the datastore where to get his data from
        root: 'results',
        totalProperty: 'total',
        id: 'id'
      },[ 
        {name: 'IDpresident', type: 'int', mapping: 'IDpresident'},
        {name: 'FirstName', type: 'string', mapping: 'firstname'},
        {name: 'LastName', type: 'string', mapping: 'lastname'},
        {name: 'IDparty', type: 'int', mapping: 'IDparty'},
        {name: 'PartyName', type: 'string', mapping: 'name'},
        {name: 'TookOffice', type: 'date', mapping: 'tookoffice'},
        {name: 'LeftOffice', type: 'date', mapping: 'leftoffice'},
        {name: 'Income', type: 'float', mapping: 'income'}
      ]),
      sortInfo:{field: 'IDpresident', direction: "ASC"}
    });
PresidentsColumnModel = new Ext.grid.ColumnModel(
    [{
        header: '#',
        readOnly: true,
        dataIndex: 'IDpresident', // this is where the mapped name is important!
        width: 10,
        hidden: false
      },{
        header: 'T&iacute;tulo',
        dataIndex: 'titulo',
        width: 250,
        editor: new Ext.form.TextField({  // rules about editing
            allowBlank: false,
            maxLength: 20,
            maskRe: /([a-zA-Z0-9\s]+)$/   // alphanumeric + spaces allowed
          })
      },{
        header: 'Autores',
        dataIndex: 'autores',
        width: 150,
        editor: new Ext.form.TextField({
          allowBlank: false,
          maxLength: 20,
          maskRe: /([a-zA-Z0-9\s]+)$/
          })
      },{
        header: 'Editora',
        readOnly: true,
        dataIndex: 'editora',
        width: 50,
        hidden: true                      // we don't necessarily want to see this...
      },{
        header: 'Categoria',
        dataIndex: 'categoria',
        width: 100,
        readOnly: true
		},{
        header: 'Assunto',
        dataIndex: 'assunto',
        width: 200,
        readOnly: true
		
      },{
        header: "Valor",
        dataIndex: 'valor',
        width: 60,
        renderer: function(v){ return '$ ' + v; },   
                           // we tell Ext how to display the number
        editor: new Ext.form.NumberField({
          allowBlank: false,
          decimalSeparator : ',',
          allowDecimals: true,
          allowNegative: false,
          blankText: '0',
          maxLength: 11
          })
      }]
    );
    PresidentsColumnModel.defaultSortable= true;
PresidentListingEditorGrid =  new Ext.grid.EditorGridPanel({
      id: 'PresidentListingEditorGrid',
      store: PresidentsDataStore,     // the datastore is defined here
      cm: PresidentsColumnModel,      // the columnmodel is defined here
      enableColLock:false,
      clicksToEdit:1,
      selModel: new Ext.grid.RowSelectionModel({singleSelect:false})
    });
    
  PresidentListingWindow = new Ext.Window({
      id: 'PresidentListingWindow',
      title: 'Livros',
      closable:false,
      width:800,
      height:450,
      plain:true,
      //layout: 'fit',
      items: PresidentListingEditorGrid  // We'll just put the grid in for now...
    });
  
  //PresidentsDataStore.load();      // Load the data
  PresidentListingWindow.show();   // Display our window

  });